PHP add/remove values from array created by json - php

I have an array that looks like this after using json_decode() on a response from a Json webservice:
[11] => Array
(
[0] => 80B37803-6278-5351-BC7A-D3A2FBFF8AA7
[1] => test
[2] =>
)
[12] => Array
(
[0] => 70B37803-6278-5351-BC7A-D3A2FBFF8AA8
[1] => test 2
[2] =>
)
[13] => Array
(
[0] => 90B37803-6278-5351-BC7A-D3A2FBFF8AA9
[1] => test 3
[2] =>
)
To print the array I use the following code:
echo '<pre>'; print_r($responseArticle); echo '</pre>';
How can I edit this kind of array in order, for example, to add a 3rd row or delete one of the already exsisting row?

$newArray = json_decode($json_data);
add inner data
adding a column for 11 number of row
$newArray[11][4] = 'this is the 4th column';
for delete
unset($newArray[11][4]);
for adding row
$newArray[lastindex] = array('90B37803-6278-5351-BC7A-D3A2FBFF8AA9','test4','');
for delete row
unset($newArray[index]);

to add another value of an array is to use array_push and to delete it you can use unset. See the example below.
<?php
$json_array[11] = array('80B37803-6278-5351-BC7A-D3A2FBFF8AA7','test','');
$json_array[12] = array('70B37803-6278-5351-BC7A-D3A2FBFF8AA8','test 2','');
$json_array[13] = array('90B37803-6278-5351-BC7A-D3A2FBFF8AA9','test 3','');
array_push($json_array, array('90B37803-6278-5351-BC7A-D3A2FBFF8AA9','test 4',''));
echo '<pre>';print_r($json_array);echo '</pre>';
unset($json_array[14]);
echo '<pre>';print_r($json_array);echo '</pre>';
?>

To add in some info for N key:
$responseArticle[N] = array('80B37803-6278-5351-BC7A-D3A2FBFF8AA7', 'testN', '');
To delete info stored in N key:
if(isset($responseArticle[N]) {
unset($responseArticle[N]);
}
Here N can be any number, say 3 as you have asked in the question.

Related

PHP Two Arrays - If they are different then insert - Otherwise Update

I have some data, one array from CSV File, another array contains a value of a field in a post.
With this data, i would like to check if the value of the field is in the CSV then update the field, if not and there is a difference to insert the field into a post.
$csv_data array has lots more fields than $vrm array. I think I need a function that says, if element is in array then, however I am unsure how to do this.
Here is what I have so far:
$vrm = [];
foreach($vehicles as $vehicle) {
$vrm[] = $vehicle->REGISTRATION;
}
$difference = array_diff($csv_data, $vrm);
if(empty($difference)) {
echo "Need to Update";
} else {
echo "Need to insert";
}
Is anyone able to assist me with this or point me in the right direction please?
** EDIT **
Array $vrm is:
Array ( [0] => CV56IPG,
[1] => RT56KLP,
[2] => AB12HNJ)
Array $csv_data is:
Array ( [0] => Array (
[REGISTRATION] => CV56IPG
[MAKE] => Volkswagen
[MODEL] => Polo
[DERIVATIVE] => 1.2 Match 5dr
)
[1] => Array (
[REGISTRATION] => AB12HNJ
[MAKE] => Volkswagen
[MODEL] => Polo
[DERIVATIVE] => 1.2 Match 5dr
)
)
I don't exactly understand your word.
I guess, If you have $vrm unique id, If you use haspMap, you can do this.
{
vrm_id1: vrm_data1,
vrm_id2: vrm_data2
}
if you insert or update new data.
when vrm_id is not exist => insert.
when vrm_id is exist => update.

Print array key value

I have an array which i get like this:
$tet = $SimplicateApi->makeApiCall('GET','/crm/person?q[first_name]=Kevin1');
I wanted to see all the array keys inside the array so i did
print_r(array_keys($tet['data']['0']));
The Results:
Array (
[0] => id
[1] => interests
[2] => simplicate_url
[3] => avatar
[4] => linked_as_contact_to_organization
[5] => gender
[6] => first_name
[7] => family_name
[8] => full_name
[9] => email
[10] => phone
)
My question is how do i check whats inside in for example first_name
array_keys($array) returns an array of $array keys.
Since the result of array_keys($tet['data']['0']) contains first_name, we can access the value with the [] operator as follows:
print_r($tet['data']['0']['first_name']);
In this code we access $tet['data']['0'] array by 'first_name' key.
var_dump full array and see all keys and their values in one shot:
echo '<pre>';
var_dump($tet['data']['0']);
echo '</pre>';
To have more insight, var_dump original array to get full info about it:
echo '<pre>';
var_dump($tet);
echo '</pre>';
So that you may know why you have to use data key and the 0 key.
<pre> tag is just used to have nice output.
I hope it helps

show random values from array of array

Below is a array generated by a query builder.
$random_array = Array ( [0] => Array ( [text] => A great time was had by all! )
[1] => Array ( [text] => KILL SHOT )
[2] => Array ( [text] => How is it possible)
[3] => Array ( [text] => http://www.youtube.com/watch?v=KwGOZpbxU9g )
[4] => Array ( [text] => http://www.youtube.com/watch?v=KwGOZpbxU9g )
)
Currently i am doing like this to print the random value
print_r(array_rand($random_array,1));
This is printing the array key as 3 or 1 etc(random from above array).I want to print the value of the key and not the key.
e.g I want to print the random value like this "http://www.youtube.com/watch?v=KwGOZpbxU9g" or "A great time was had by all!" instead of 3 or 1 which is printing now.
Is it possible to do this.
You will have one more line of code as shown below:
$array_key = array_rand($random_array,1); //get the key
print_r( $random_array[$array_key] ); //use the key to print value
What about simply calling
$randNumber = rand(0,count($random_array))-1; //index in array starts with 0
print (string) $random_array[$randNumber];

php multidimensional array problems

I want to output only 1 name data from my array which is:
Array
(
[results] => Array
(
[0] => Array
(
[0] => some name
[1] => Founder
)
[1] => Array
(
[0] => some name
[1] => Marshal
)
[2] => Array
(
[0] => some name
[1] => Marshal
)
[3] => Array
(
[0] => some name
[1] => Royal Knight
)
[4] => Array
(
[0] => some name
[1] => Knight
)
)
)
1
now I use :
echo "<pre>";
echo print_r($API->getSearchFreeCompanyMembers());
echo "</pre>";
to print the array which seems to work fine but when I try selecting single array like:
echo "<pre>";
echo print_r($API->getSearchFreeCompanyMembers(1)[0]);
echo "</pre>";
all I get is 1 on the page
Any help would be much appreciated, if any more code needed please let me know.
You seem to have changed the arguments when calling the method. getSearchFreeCompanyMembers
Your first example shows getSearchFreeCompanyMembers()
The second is getSearchFreeCompanyMembers(1)
To get the first element in the array returned by the method.
1. Dereference as you did (just do not put 1 as argument).
$result = $API->getSearchFreeCompanyMembers()[0]
Beware the side effect is that the rest of the array returned is discarded. Also this feature is only available from <= 5.4
2. Save the returned array to a variable and pick the first element
$array = $API->getSearchFreeCompanyMembers();
print_r($array[0]);
For more info on arrays and for dereference see example #7
http://php.net/manual/en/language.types.array.php
You may try something like this
$array = $API->getSearchFreeCompanyMembers();
echo $array['results'][0][1]; // first name (Founder)
echo $array['results'][1][1]; // second name (Marshal)
Or, use (for some name)
echo $array['results'][0][0]; // first ('some name')
echo $array['results'][1][0]; // second ('some name')
Here, results is an associative array and the first array is [0] and every array ([0], [1]) in results contains an array. So, it's something like this
$array = Array(
'results' => Array (
Array ( 'some name', 'Founder' ), // 1st array in results
Array ( 'some name', 'Marshal' ) // 2nd array in results
)
);
echo "<pre>";
$members = $API->getSearchFreeCompanyMembers();
print_r($members['results'][0]); //no echo needed
echo "</pre>";
like to print array results key [0] value [1] i.e founder. you have to call array like this
before print_r there is no need of echo
echo "<pre>";
print_r($results [0][0]);
echo "</pre>";
similarly for
echo "<pre>";
$employee=$API->getSearchFreeCompanyMembers();
print($employee['results'][1][0]);
echo "</pre>";

php array merge with value of parent array

I need to merge an array with value of parent array.
$testArr=unserialize('a:6:{s:5:"queue";a:2:{i:6;s:1:"5";i:5;s:1:"2";}s:3:"sum";a:2:{i:6;s:3:"765";i:5;s:3:"2.1";}s:7:"sumAccD";a:2:{i:6;s:3:"543";i:5;s:3:"3.1";}s:7:"sumAccC";a:2:{i:6;s:2:"54";i:5;s:3:"3.3";}s:7:"comment";a:2:{i:6;s:12:"test comment";i:5;s:6:"111222";}s:3:"yt0";s:0:"";}');
$ret = array();
foreach ($testArr as $pkey => $pval) {
if (is_array($pval)) {
foreach ($pval as $pvkey => $pvval) {
$ret[$pvkey] = array($pkey => $pvval);
}
}
}
echo '<pre>', print_r($ret), '</pre>';
In this case it prints out
Array
(
[6] => Array
(
[comment] => test comment
)
[5] => Array
(
[comment] => 111222
)
)
1
Unfortunally it print out only comment. I need to add other rows: queue,sum,sumAccD,sumAccC. Array must look like this:
Array
(
[6] => Array
(
[queue] => 5
[sum] => ''
....
[comment] => test comment
)
[5] => Array
(
[queue] => 2
[sum] => 2.1
....
[comment] => 111222
)
)
1
Please help merge them.
Thanks.
Look at this line:
$ret[$pvkey] = array($pkey => $pvval);
You're assigning the key to a new array every time, overwriting what was previously there.
In your case, 'comment' is the last key that is processed, so that's going to be the only key in the final array.
Instead of this, you could define a new array only once outside the inner for, like this:
$ret[$pvkey] = array();
And then assign your values to that array in the inner for loop as you would normally do (so no more creating arrays there!)
Problem solved by replacing
$ret[$pvkey] = array($pkey => $pvval);
with
$ret[$pvkey][$pkey] = $pvval;

Categories