Print array key value - php

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

Related

How do I change the array value?

Is it possible to change the highlighted word to numbers without changing it in database table?
wanted from this
$value['how']
to this
$value['0']
Yes, you need to use array_values()
$array = array("how" => "how-value", "to" => "to-value");
print_r(array_values($array));
Output:
Array
(
[0] => how-value
[1] => to-value
)
EDIT BY OP
To get the value
foreach($array as $key => $value) {
$someArray = array_values($value);
print_r($someArray[0]);
}
//return 144
#Milan Chheda answer is correct but I am just briefing here so the user can get a better idea of that.
Use array_values() to get the values of an array.
$FormedArray = array_values($your_array);
now print that array print_r($FormedArray);
You will get your result like
Array
(
[0] => 144
[1] => 41
[2] => USA
[3] => 12
[4] => 12
)
Here is a working example. https://eval.in/843720

PHP add/remove values from array created by json

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.

Accessing Array Data in Object

I'm trying to access a piece of data in an array of arrays that (I believe) is in an object (this may not be the right term though).
When I do print_r on this: $order_total_modules->process() I get...
Array (
[0] => Array (
[code] => ot_subtotal
[title] => Sub-Total:
[text] => $49.99
[value] => 49.99
[sort_order] => 1
)
[1] => Array (
[code] => ot_total
[title] => Total:
[text] => $0.00
[value] => 0
[sort_order] => 12
)
)
If I run echo $order_total_modules->process()[1][3];, I should get "0", because that is the 3rd element of the 2nd array... right? Yet, I get an error.
Can anyone help with this?
Even though it is the third element counting from 0, the index is not 3 it is an associative array with the index value:
Available in PHP >=5.4.0:
echo $order_total_modules->process()[1]['value'];
Or PHP < 5.4.0:
$result = $order_total_modules->process();
echo $result[1]['value'];
You cannot access an associative array via an integer index(unless the index is an actial integer).
So in this case use :
[1]['code'] to access what woulde be [1][0] with a 'normal' array.
Try putting it in a var first:
$ar = $order_total_modules->process();
echo $ar[1]['value'];
The second level array is an assoc, which means that the key is not numeric, which means that you need to call the name of the key, hence the 'value'.

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>";

Displaying two arrays in one foreach loop

I am making use of two arrays in one foreach loop.
Here's the code snippet that i have written for two input file type arrays.
$file[]= $_FILES['f_name']['name'];
$tmp_name[]=$_FILES['f_name']['tmp_name'];
foreach (array_combine($file, $tmp_name) as $code => $name) {
print_r($code);
print_r($name);
}
The resultant value i get on printing the array is this:
ArrayArray ( [0] => C:\xampp\tmp\phpC24D.tmp [1] => C:\xampp\tmp\phpC24E.tmp [2] => C:\xampp\tmp\phpC25F.tmp [3] => C:\xampp\tmp\phpC260.tmp [4] => [5] => [6] => [7] => [8] => [9] => [10] => )
It prints only one array, doesn't prints the other array.
How can I get it printed?
thanks in advance.
Actually it is printing 2 arrays, just the 1st one contains nothing.
ArrayArray (
I prefer to use var_dump compared to print_r as it gives you more details for debugging.
As you are combining the 2 arrays into one and they only have 1 index there is nothing in the $code variable, just $name - unless you add array indexes.

Categories