user2179427 user2179427 - 5 months ago 14
PHP Question

php addressing items in array by index

I am trying to address particular item from array by the index of the item because. I have tried extract method, but I do not have my array defined as follows:

$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");


Instead I have:

$contents = array();


which is filled in a
while loop
.

In
c#
I would do for example something like this:

string variable = contents[3];


How can I achieve something like above in
PHP?


EDIT:
When i do this:

$param1= $contents[0];
echo "$param1";


I get this:


Notice: Array to string conversion in C:\xampp\htdocs\NUSYS\pars.php
on line 45 Array


Edit 2

this shows
print_r($contents);


Array ( [0] => Array ( [0] => bsd [1] => ghj ) )

Answer

You need to do it like below:-

$contents = Array ( [0] => Array ( [0] => bsd [1] => ghj ) ) ;//as you shown

$param1= $contents[0][0];
echo $param1; //outputs `bsd`

Output:-

  1. https://eval.in/597650
  2. https://eval.in/597663

Note:- Start learning php basics.

Lot of other possibility are there to do the same thing like:-

$data = array_values($contents[0]);
echo $data[0];

Output:- https://eval.in/597673.