0

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 ) ) 
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
user2179427
  • 331
  • 5
  • 16
  • 1
    If I'm understanding the question wouldn't it just be: `$myArray[0]` or whatever index you want? If it is an associative array and you want numerical indexes try [`array_values`](http://php.net/manual/en/function.array-values.php). – Script47 Jun 29 '16 at 11:57
  • 1
    Please, refer to [**php.net->Arrays**](http://php.net/manual/en/language.types.array.php). – FirstOne Jun 29 '16 at 11:58
  • @Anant Take a look to my EDIT2 please. – user2179427 Jun 29 '16 at 12:04
  • You have array inside array – newage Jun 29 '16 at 12:05
  • 1
    @user2179427 your edit suggests that you don't have the basic understanding of PHP. You should consider reading the documentation available. – Script47 Jun 29 '16 at 12:05

3 Answers3

2

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.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
0
$var = $var_array[index];

In your example:

$ var = $var_array[3] Although index 3 would be empty cause you start counting from 0 and u have 3 items so the last item is index 2

Edit: Can you upload your entire code? Array + echo? Cause are you having array in your array? or jsut strings?

Roy Stijsiger
  • 199
  • 2
  • 12
0

if you want the nth value then use array_values to convert your named keys into numeric keys (in a copy of the original array)

something like this:

$arr = array(); // your initially empty array
fill_it(); // some processing fills it with key=>value pairs
$item = array_values($arr)[2]; // pick up the third key (zero-indexed, then 2 is the 3rd key)
var_dump($item); // output to see it

and for completeness' sake, the fill_it() function might be like the following:

// aux func
function fill_it() {
    global $arr;
    // dynamically fill the array
    $keys = array('color', 'size', 'shape');
    $values = array('blue', 'medium', 'sphere');
    foreach ($keys as $key) {
        $arr[$key] = array_shift($values);
    }
    //var_dump($arr);
}
arhak
  • 2,488
  • 1
  • 24
  • 38