PHP array difference in value geting - php

Can someone explain to me by the most easiest way possible difference between following? After reading about PHP arrays, i still dont get it.
print $myArray[0]->token
and
print $myArray[0]["token"]
Edit:
Question is not about best approach, but about meaning of that. Answer can be fond here, but it is not direct answer to my question

An example of all this :
<?php
//Creating simple object
$bookObject = new stdClass;
$bookObject->title = "Harry Potter and the Prisoner of Azkaban";
//Adding object to books array
$books = array($bookObject);//this array is equivalent to your $myArray
//Acessing object using -> operator
echo $books[0]->title;
//Re-initializng books array
$books = array(0=>array("title" => "Harry Potter and the Prisoner of Azkaban"));
//Accessing elements of an array by key
echo $books[0]['title'];
?>

$myArray is an array having an object with the property (attribute, variable) named token as its first element.
$myArray is an array having an associative array with the key named token as its first element.
So, it's about two different data structures that the array is holding as the first element indexed by 0.

An object inside an array having token element
An array inside an array having token element

Related

fetch value from an array

I need to fetch value from an array .I need to fetch the value name,value,user_id from an given array
$inner_content='[{"name":"radio","value":"1","id":"1","user_id":"admin#gmail.com","web":"571710720","type":"poll_info","pg":"question_response"},{"name":"fav-color[]","value":"blue"}]'
$id='5'; //value given for expample.
$inner="select * from user_response where POLL_ID=$id";
$inner1=mysql_query($inner);
while($ifet=mysql_fetch_assoc($inner1))
{
$inner_content = $ifet['CONTENT_VALUES'];
$data1 = json_decode($inner_content);
$test1[]=array('name'=>$data1->name);
}
In JSON, square brackets denote an array, and curly braces denote an object. As you can see if you look carefully at $inner_content, it's an array containing a bunch of objects, so you need to index it.
$test1[] = array('name' => $data1[0]->name);
This just gets the name from the first object in the array. If you want to get all the names, you could use a foreach loop on $data1 (but only the first one has all the properties that you say you want).

Add an array elements into PHP/ Insert variable with value with Array_push

I am able to parse data from youtube gdata. For example:
$json_output = json_decode($json,TRUE);
foreach ( $json_output['data']['items'] as $data ){
echo $data['content'][1]
. '</br>'
. $data['title'];
These give me rtsp url and title. Now suppose I want some other elements to add to the output (I don't possess knowledge of php, so I don't know the terms actually). What I want, that the output should have an another variable, let's take 'Boys'. This variable will have index key values same as youtube gdata and will be as follows:
Boys="You", "Me", "He", "Doggy",.....nth value.
My above code gives two values each time-url and title. Now I want the 3rd value which will be added to that from 'Boys'.
I tried Array_push as follows but it adds as an extra element and not as a variable like 'title' or 'content'.
$Boys= array('demi', 'ffe', 'erere');
array_push($json_output['data']['items'], $Boys );
How to properly insert Boys as variable? is there other methods like merge etc. to do it?
Again, since I am not a coder, please pardon my words!
Let's say you have 2 arrays with same keys, then you can do array_merge_recursive() to merge them together.
Here is a working example,
$a = [ 'boys' => ['play', 'sing']];
$b = ['boys' => 'fight'];
$merged = array_merge_recursive($a, $b);
print_r($merged);

Acessing an multidimentional array from a variable

I want to store the parents keys of an array so I can access it later.
Something like:
$arr['hello'][0]['world'] = 'a';
$arr['hello'][1]['world'] = 'b';
And store both hello, 0 and world as some kind of variable so I can access the array with it:
For example, something I would think it may work is:
$indexes = array('hello', 0, 'world');
$arr[$indexes]
But this doesn't work, as an array is an illegal offset type for another array. So is there a way to access an array by an array of parents keys (variable)?
i think you want
echo $array[{$one}][{$two}];
So do you want to access a child array by a custom key that occurs in a parent array?
$parent_array[$custom_key] = array('hello',0,'world');

PHP Arrays Accessing title name of array object

I am trying to get the title name of an array object.
For example I have
$results[0]['page']
$results[0]['title']
I was wondering if I could use a for loop to loop through the $results[0] array and get the 'page' and 'title' names and insert them into a new array?
Thanks for your time
foreach ($results[0] as $key=>$value) {
//Your key will be page and title, and then the value will be in value
}
More information on the PHP documentation for foreach().
Or you could just use array_keys() to turn the assoc indexes into a new array automatically.

php arrays (and removing certain element)

I'm not 100% but this ($settings) would be called an array in php:
$setting;
$setting['host'] = "localhost";
$setting['name'] = "hello";
but what's the name for this that's different to the above:
$settings = array("localhost", "hello");
Also from the first example how can i remove the element called name?
(please also correct my terminology if I have made a mistake)
I'm not 100% but this ($settings)
would be called an array in php:
You should be 100% sure, they are :)
but what's the name for this that's
different to the above:
This:
$setting['host'] = "localhost";
$setting['name'] = "hello";
And this are different ways of declaring a php array.
$settings = array("localhost", "hello");
In fact this is how later should be to match the first one with keys:
$settings = array("host" => "localhost", "name" => "hello");
Also from the first example how can i
remove the element called name?
You can remove with unset:
unset($setting['name']);
Note that when declaring PHP array, do:
$setting = array();
Rather than:
$setting;
Note also that you can append info to arrays at the end by suffixing them with [], for example to add third element to the array, you could simply do:
$setting[] = 'Third Item';
More Information:
http://php.net/manual/en/language.types.array.php
As sAc said, they are both array. The correct way to declare an array is $settings = array(); (as opposed to just $settings; in your first line.)
The main difference between the first and second way is that the first allows you to use $settings['host'] and $settings['name'], whereas the latter can only be used with numeric indices ($settings[0] and $settings[1]). If you want to use the first way, you can also declare your array like this: $settings = array('host'=>'localhost', 'name'=>'hello');
More reading on PHP arrays
Well this is indeed an array. You have different types of array's in php. The first example you mention is called an Associative Array. Simply an array with a string as a key.
An associative array can be declared in two ways:
1) (the way you declared it):
$sample = array();
$sample["name"] = "test";
2)
$sample = array("name" => "localhost");
Furthermore the first example can also be used to add existing items to an array. For example:
$sample["name"][] = "some_name";
$sample["name"][] = "some_other_name";
When you execute the above code with print_r($sample) you get something like:
Array ( [name] => Array ( [0] => some_name [1] => some_other_name ) )
Which is very usefull when adding multiple strings to an existing array.
Removing a value from an array is very simple,
Like mentioned above, use the unset function.
unset($sample["name"])
to unset the whole name value and values connected to it
Or when you only want to unset a specific item within $sample["name"] :
unset($sample["name"][0]);
or, ofcourse any item you'd like.
So basicly.. the difference between your first example and the latter is that the first is an associative array, and the second is not.
For further reference on arrays, visit the PHP manual on arrays

Categories