0

I have array like this

$array =  (
            [customer] => Array
                (
                    [emailAddress] => test@gmail.com
                )

            [recommendationProductDetails] => Array
                (
                    [0] => Array
                        (
                            [productId] => 7053
                            [title] => silikonska termalna pasta tuba 7gr
                            [imageWebAddress] => http://Image/GetImage/6118
                            [webAddress] => http://Shop/Product?name=AG%20Termopasty%20silikonska%20termalna%20pasta%20tuba%207gr&accountingCode=8703
                            [description] => silikonska termalna pasta tuba 7gr
                        )

                    [1] => Array
                        (
                            [productId] => 4610
                            [title] => silikonska termalna pasta tuba 7gr
                            [imageWebAddress] => http://Image/GetImage/6118
                            [webAddress] => http://Shop/Product?name=AG%20Termopasty%20silikonska%20termalna%20pasta%20tuba%207gr&accountingCode=8703
                            [description] => silikonska termalna pasta tuba 7gr
                        )

                    [2] => Array
                        (
                            [productId] => 4591
                            [title] => silikonska termalna pasta tuba 7gr
                            [imageWebAddress] => http://Image/GetImage/6118
                            [webAddress] => http://Shop/Product?name=AG%20Termopasty%20silikonska%20termalna%20pasta%20tuba%207gr&accountingCode=8703
                            [description] => silikonska termalna pasta tuba 7gr
                        )

                    [3] => Array
                        (
                            [productId] => 1756
                            [title] => silikonska termalna pasta tuba 7gr
                            [imageWebAddress] => http://Image/GetImage/6118
                            [webAddress] => http://Shop/Product?name=AG%20Termopasty%20silikonska%20termalna%20pasta%20tuba%207gr&accountingCode=8703
                            [description] => silikonska termalna pasta tuba 7gr
                        )

                )

        )

I need to get new array that will look like this

    $newArray = [7053, 4610, 4591, 1756];

I have tried a lot of solution from this question Is there a function to extract a 'column' from an array in PHP? But i have no luck, also i have tried to foreach all like this

    foreach($array as $row => $innerArray){
        foreach($innerArray as $innerRow => $value){
          print_r($value) . "<br/>";
        }
      }

But this way i got only display of results, also i have tried answers from this question Foreach for arrays inside of an array

After all that i didnt manage to get proper new array, can somebody help me pleae, because I am not so good at php. Thanks in advance

Also i have tried

foreach ($dataRecommend as $item) {
    $namearray[] = $item['recommendationProductDetails'];
}

foreach ($namearray as $item) {
    $productArray[] = $item['productId'];
}
print_r($productArray);

Also i made it like this

foreach ($array as $item) {
    $namearray[] = $item['recommendationProductDetails'];
}
for($i = 0, $l = 4; $i < $l; ++$i) {
    foreach ($namearray as $item) {
 $productArray[] = $item[$i]['productId'];
 }
  }

print_r($productArray);

But i dont see this is good :(

Miomir Dancevic
  • 6,726
  • 15
  • 74
  • 142
  • Create a new empty array. Then instead of displaying the results, add the results to that new array. – RST May 27 '20 at 10:33
  • `foreach` through `$array[recommendationProductDetails]` and add the value of `productId` to a new array – brombeer May 27 '20 at 10:34
  • _“I have tried a lot of solution from this question”_ - then _show us_ what you actually tried, instead of just going “no luck”. (And please provide example data as a `var_export` in cases like this, so that everyone can simply copy&paste it to get _working_ code right away.) – CBroe May 27 '20 at 10:34
  • Why are you so stricted, i told you i have looked answers but I dont know how to implement those answers in my code :( – Miomir Dancevic May 27 '20 at 10:41
  • @CBroe If you know simple answer maybe you can help me, because as i said I am not so good in PHP, if you want ot help me? – Miomir Dancevic May 27 '20 at 10:42
  • You said you _tried_ something, so you should _show us_ what you actually tried - as [ask] explains. Just going “I tried a bunch of stuff, but it all did not work” is _not_ a proper way to ask here on this site to begin with. _“If you know simple answer”_ - the simple answer is in what you already found - `array_column`. – CBroe May 27 '20 at 10:46
  • @CBroe I have added edit what i have tried – Miomir Dancevic May 27 '20 at 10:49
  • @kerbh0lz Can you please make answer, thanks – Miomir Dancevic May 27 '20 at 11:05

3 Answers3

0

Is there a function to extract a 'column' from an array in PHP?

Yes, there is array_column(array $input, mixed $column_key [, mixed $index_key = NULL]) doc

So in your code: array_column($array['recommendationProductDetails'], 'productId');

Justinas
  • 41,402
  • 5
  • 66
  • 96
  • Notice: Undefined index: recommendationProductDetails – Miomir Dancevic May 27 '20 at 10:39
  • @MiomirDancevic then your `$array` appears to be something different, than what you have shown us as example data above. – CBroe May 27 '20 at 10:47
  • This comes from $array = json_decode( $bodyRecommend, true); – Miomir Dancevic May 27 '20 at 10:50
  • It doesn’t matter where it comes from, what matters is what the structure actually is. And according to what you have shown in your question, this should work. If it doesn’t - then your data must be somehow different from what you have shown us. – CBroe May 27 '20 at 11:02
  • I have updated my question, and found stupid solution that work, can you please help me refactor – Miomir Dancevic May 27 '20 at 11:02
0

array_column(array $input, mixed $column_key [, mixed $index_key = NULL]) doc

array_column($array['recommendationProductDetails'], 'productId');

0

You can use foreach to iterate over every item in $array['recommendationProductDetails']. $newArray will hold the extracted values:

$newArray = array();
foreach ($array['recommendationProductDetails'] as $arrayItem) {
    array_push($newArray, $arrayItem['productId']);
}
print_r($newArray);

will output

Array
(
    [0] => 7053
    [1] => 4610
    [2] => 4591
    [3] => 1756
)
brombeer
  • 8,716
  • 5
  • 21
  • 27