1

I have an associative array which is generated dynamically with the values from database. When I print the whole array, it gives something like this when we put print_r($array).

  Array ( [95a5c80811239526fb75cbf31740cc35] => Array ( [product_id] => 2324) )

When I echo like this,

echo $array['95a5c80811239526fb75cbf31740cc35']['product_id'];

it gives me product id. But the problem is, the code '95a5c80811239526fb75cbf31740cc35' changes dynamically everytime. I want to echo the product id irrespective of this code.

I tried

$array[]['product_id'];
$array['']['product_id'];

But not working. Can anyone help me? Please ask me if you have any doubts.

1
  • Is there always only a single element in the array? Commented Jul 11, 2014 at 8:16

6 Answers 6

2

You can use reset() in this case:

$array = array(
    '95a5c80811239526fb75cbf31740cc35' => array( // dynamic
        'product_id' => 2324
    ),
);

$value = reset($array); // set pointer to first element
echo $value['product_id']; // 2324
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming that the code is always the first element in the array:

$array[0]['product_id'];

If you collectively want all of the product ID's:

foreach($array as $product){
    $productIds[] = $product['product_id'];
}

// $productIds is now what $array was, but without the codes, so the product_id's are the first elements.

Comments

1

You can use for each for this so that you can get the value of product Id

 $array = Array ( [95a5c80811239526fb75cbf31740cc35] => Array ( [product_id] => 2324) )

foreach($array as $product){
echo $product['product_id'];
}

This would get your desired o/p

Comments

1

IF you are getting problem using Associative array then you can first convert it into numeric as follows

    $arr=array( 'first' => array( 'product_id' => 2324) );
    $arrr=array_values($arr);
     echo $arrr[0]['product_id'];

Output:

2324

Hope this helps and to know about array_values go here

Comments

1

Depending on your situation, there are few possible solutions:

$array = array_shift(array_values(
  Array(
    '95a5c80811239526fb75cbf31740cc35' =>
       Array(
          'product_id' => 2324
       )
    )));

echo $array['product_id']; // 2324

Another solution, probably more efficient:

echo array_shift(array_slice($array, 0, 1)); // 2324

For PHP 5.4+ you could go with:

echo array_values($array)[0]; // 2324

2 Comments

This seems a long winded way to what is simply extracting the 2nd element of an array
What is so long in doing: array_values($array)[0]; The other is just study examples.
-1
$array[0]['product_id'];

Should do the trick.

1 Comment

This will return a value, but it's not clear from the question if it will be the right one. It might be safer to use array_shift to get the first item without using an index at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.