How to get particular value in this array in php - php

How to get particular value in this array in PHP
[{"key1":"value1","key2":"value2"}]

You can use to find specific key with value by using array_search function
$json = '[{"key1":"value1","key2":"value2"}]';
$array = json_decode($json, true);
echo array_search("value2", $array[0]); //key2
echo $array[0]["key2"] //value2

You can get specific value from the array by using PHP array_search() Function. Below is an example code that you need to follow,
Since provided PHP array is in JSON format you first need to decode it as follows,
$jsonArray = '[{"key1":"value1","key2":"value2"}]';
$array = json_decode($jsonArray, true);
So the decoded array will be like this,
Array
(
[0] => Array
(
[key1] => value1
[key2] => value2
)
)
And then you can use array_search() Function,
<?php
$jsonArray = '[{"key1":"value1","key2":"value2"}]';
$array = json_decode($jsonArray, true);
echo array_search("value1", $array[0]); // Search an array for the value "value1" and return its key. In this case the key will be "key1"
?>
Note:
array_search() function is the best bet. But you also can use a for-loop to iterate all values and check if matches the value that you need. But I recommend using array_search() function.
Hope this helps you!

Related

Store array data in single line PHP

Hello i want to store my array data into variable.
$array = array('0' => "14254",'1' => "145245");
// I want to store this array value into normal single variable
// store array data to $string.
// something like this >> $string is 14254,145245
Use array's implode() method.The implode() function returns a string from the elements of an array.
<?php
$array = array('0' => "14254",'1' => "145245");
$script = implode(',',$array);//outputs 14254,145245
echo $script;
?>
For more see manual PHP Implode
Look at 'serialize' function in PHP. It will provide you array as a string that later could be converted back to array with 'unserialize'

How can make array of string

$var = {"4":true,"6":true,"8":true}
In The above string I want to get numbers into array.
Need: $var2 = [[0]=>4, [1]=>6, [2]=>8];
All response will be appreciated.
You should use json_decode and array_keys to accomplish it:
array_keys(json_decode($var, true));
First you decode the string using json_decode, the second argument means that the function should return an associative array and non an array of objects. This willl help up get the array keys.
$decoded = json_decode($var, true);
You get the array keys with this loop and place them in $var
foreach($decoded as $key => $value){
$var2[] = $key;
}
As i comment, use array_keys, and json_decode.
I don't believe that this question has an answer, So i did't answer
it. But i did it later.
You have an json, so you need to use json_decode now you jave an array where your keys are the desired value. so use array_keys.
$var = '{"4":true,"6":true,"8":true}';
$arr = json_decode($var, true);
echo '<pre>';
print_r(array_keys($arr));
Result:
Array
(
[0] => 4
[1] => 6
[2] => 8
)

jQuery to PHP Array and string

I'm trying to get the checked values into a PHP array so that I am able to loop through the array but I can't seem to convert it to an array.
jQuery Ajax Posts
checked=28,24
PHP
$checked = $_POST['checked'];
$arr = array($checked);
print_r($arr);
OUTPUT
Array
(
[0] => 28,24
)
Use explode function to convert comma separated string to an array,
$arr = explode(",",$checked);

Append field to the end of an array

I have an array, and when i echo it i get the following output;
Array[{"name":"Kat","age":"10"}]
Now, i need to add an additional filed into this; so finally it should appear as;
Array[{"message":"Success","name":"Kat","age":"10"}]
if $arr is my array, how am i going to append "message":"Success" ?
Sorry, i don't have any code to demonstrate my workings, i am stuck here. i would appreciate it if anyone can help me.
Your array content looks like JSON to me. But, if your array are actually just PHP arrays, then do:
$arr = array('name' => 'Kat', 'age' => '10');
$arr['message'] = 'Success';
If it is a JSON encoded array:
$arr = json_decode('{"name":"Kat","age":"10"}' , true)); //true decodes to an array and not a standard object
$arr['message'] = 'Success';
echo $arr;
//If you want it back in JSON
$json = json_encode($arr);
echo $json;
Like Waygood said if you want to add a value to the end of an array just use:
$array[] = $value; or $array['somekey'] = $somevalue;
However, if you need to add a value to the beginning of the array (like your example) you can use:
array_unshift($array, $value);
Alternatively, if you need to add a key and a value to the beginning you can simply make an array with the key => value pair and merge the two arrays like so:
$firstArray = array("message" => "Success");
$newArray = array_merge($firstArray, $secondArray);
For reference, here are the links to the php.net documentation:
array_unshift
array_merge
what about $arr["message"]="Success";?
To add a named field, you can just use this:
$array['message'] = 'Success';
To add a unnamed field, this is the way to do it:
$array[] = $value;
There are multiple ways of doing it.
PHP array functions
http://php.net/manual/en/function.array-push.php
http://php.net/manual/en/function.array-merge.php
The + operator
Or like others mentioned using $arg['var'] =
Just define "message":"Success" as another array and use push, merge or +
Also looks like you have it Json encoded. You will need to handle that as well.
The array functions only work with regular arrays, not encoded strings.
Try this:
$arr = array("name"=>"Kat","age"=>"10");
print_r($arr);
$arr = array_merge(array("message" => "Success"), $arr);
print_r($arr);

Convert Array into Key Value Pair array

I have a comma separated string which i explode into an array. If the array is of un-known length and i want to make it into a key value pair array where each element in the array has the same key, how do i do this? i'm assuming i'd have to use array_combine? can anyone give me an example using the array bellow? :
for instance:
array([0]=>zebra, [1]=>cow, [2]=>dog, [3]=>monkey, [4]=>ape)
into:
array([animal]=>zebra, [animal]=>cow, [animal]=>dog, [animal]=>monkey, [animal]=>ape)
You can't use the same key for each element in your array. You need a unique identifier to access the value of the array. When you use animal for all, what value should be used? What you can do is to make a 2 dimensional array that you have an array inside an array:
array(
[animals] => array(
[0]=>zebra, [1]=>cow, [2]=>dog, [3]=>monkey, [4]=>ape
)
)
this can be used with $array['animals'][0]
But still you need numbers or unique identifiers to access the values of the array.
Something like this:
$string = 'zebra,cow,dog,monkey,ape';
$array = explode(',', $string);
$arrayReturn['animals'] = $array;
print_r($arrayReturn);
u cant have same key for all the values but u can do this
lets say your string is
$a = 'dog,ant,rabbit,lion';
$ar = explode(',',$a);
$yourArray = array();
foreach($ar as $animals){
$yourArray['animals']=$animals;
}
Now it doesnot matter how long your string is you will have you array as
$yourArray['animals'][0]='dog'
$yourArray['animals'][1]='ant'
....... so on ......

Categories