php count not working properly in my array - php

i print php array when it's empty it print 1
$addresses = $user->myFunction();
print_r(count($addresses['my_test']));
Array
(
[my_test] => Array
(
[test] => test
[test1] => test1
)
)
it print 2
when i print this
Array
(
[my_test] => Array
(
[] =>
)
)
i got 1
i don't know what is the problem here
How to print exact value of this?

Array count all element including empty ones. Your output is correct as you second array has 1 element.
Consider use array_filter to avoid them.
Example:
$a = array("" => "");
echo count($a). PHP_EOL; // echo 1
$a = array_filter($a);
echo count($a). PHP_EOL; // echo 0
In you case:
print_r(count(array_filter($addresses['my_test'])));

Array
(
[my_test] => Array
(
[test] => test
[test1] => test1
)
)
print_r(count($addresses['my_test'])); // it will show 2 cause you have 2 values inside my_test key.
print_r(count($addresses)); // it will show 1 cause you have one value inside main array
Array
(
[my_test] => Array
(
[] =>
)
)
print_r(count($addresses['my_test'])); // it will show 0 because you have 0 values inside my_test key.
print_r(count($addresses)); //it will show 1 because you have one value my_test inside your main array.
Hope it helps you clarify count function.

Related

I was trying to print the value of count in my data variable in laravel, the following is the value of the variable

print_r($data) gives the below value, I am not sure about how to get the count value from the below
Array ( [result] => Array ( [0] => Array ( [_id] => [count] => 6 ) ) [ok] => 1 )
I want to get 6 as output
You may explictly access the array using its variable name then the keys
For example:
$data['result'][0]['count']

Extend assoc array

I would like to extend an assoc array.
The original array is like
$arr = [[
'ID' => 0,
'TEXT' => SOME_CONST,
'CHECKED' => $opt_flag,
]];
With a new condition I would like to extend this existing array.
if (defined('MODULE_STATUS') && MODULE_STATUS == 'true') {
$arr['JSON_ATTRDATA'] = 1;
}
The result is
Array
(
[0] => Array
(
[ID] => 0
[TEXT] => text
[CHECKED] => 1
)
[JSON_ATTRDATA] => 1
)
What is wrong that the new key is outside of the nestet array?
you want $arr[0]['JSON_ATTRDATA'] = 1; right now you appending to the parent array, you want the child array which has the key 0
the Array $arr already having 1 object
to change this object value by its index
$arr[0] = "anything else"
and to add more objects
$arr[] = "Another object";
you should Call :
$arr[0]['JSON_ATTRDATA'] = 1;
will add another key => value to first object
Do $arr[0]['JSON_ATTRDATA']
DETAILS
$arr contains array within array. Like this
Array
(
[0] => Array
(
[ID] => 0
[TEXT] => text
[CHECKED] => 1
)
)
So when you add $arr['JSON_ATTRDATA'] it add into first array

Put a key of end of an array in php

I have this array:
Array
(
[0] => Array
(
[date] => 2016-03-08
[value] => Array
(
[key_1] => Array
(
[test_1] => 1
[test_2] => 10
[test_3] => 1000
[test_4] => 200
)
[key_2] => Array
(
[test_1] => 1
[test_2] => 15
[test_3] => 1500
[test_4] => 100
)
)
)
Now I have another array :
Array
(
[key_3] => Array
(
[test_1] =>
[test_2] =>
[test_3] =>
[test_4] => 1
)
)
I want to add this last array in the first array.
I try like this : array_push($ymlParsedData[]['value'], $a_big_gift); but not work. Can you help me please ?
You can't use $ymlParsedData[] for accessing specific element, it is a shorthand for pushing data to array.
You can use either
// NB! array_push() just adds the values, key 'key_3' is removed
array_push($ymlParsedData[0]['value'], $a_big_gift);
or
// will keep key 'key_3'
$ymlParsedData[0]['value']['key_3'] = $a_big_gift['key_3'];
or
// use array_merge() instead
$ymlParsedData[0]['value'] = array_merge($ymlParsedData[0]['value'], $a_big_gift);
A complicated answer, but this might solve your issue:
$key_name = array_keys($a_big_gift)[0];
$ymlParsedData[0]['value'][$key_name] = $a_big_gift[$key_name];
echo '<pre>'; print_r($ymlParsedData); exit;
Note: For making it dynamic and for more than one value of $a_big_gift, you need to loop it and achieve your result.
Try this
array_push($ymlParsedData[0]['value'], $a_big_gift['key_3']);

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

Echo certain value from an array

I am trying to echo a certain value of an array which was outputted by an API call.
So when i call
$userid = 42
$api->listServersByOwner($userid)
will output
Array ( [success] => 1 [errors] => Array ( ) [data] => Array ( [Servers] => Array ( [259] => user42 Server ) ) )
What i specifically want is to put the "259" in a variable.
Please do note that this number changes allot depending on the userid so it wont always be 259.
Any help would be great
You mean
$array = $api->listServersByOwner($userid);
$var = key( $array['data']['Servers'] );
Replace $arr with the name of your variable.
echo key( $arr['Servers'] );

Categories