Count array keys - php

Array
(
[0] => Array
(
[name] => WWW
)
[1] => Array
(
[name] => Hi
)
[2] => Array
(
[name] => Hello
)
[3] => Array
(
[name] => World
)
)
I have the above array and I want to count the number of keys.
When using the following code
$temp = array_keys($array);
echo $temp;
the result is 2 instead of 4 (0,1,2,3). What I'm doing wrong ?

You need to count the array in order to get a number:
$arr = array
(
"0" => array
(
"name" =>"WWW"
),
"1" => array
(
"name" => "Hi"
),
"2" => array
(
"name" => "Hello"
),
"3" => array
(
"name" => "World"
)
);
$keys_count = count($arr);
echo $keys_count;

Just count the array itself: count($array).
There's always the same amount of keys as there are values!

Very simple buddy. Look this:
$array = array(0 => 100, "color" => "red");
print_r(count($array));
php.net help you for all! ;)

Related

split one array have two key split into two array in php

Question :
I have one array have two key or more split into two or more create array based on array in php.
my Array :
array
(
[RAJAHMUNDRY] => Array
(
[unspcp_code] => 46182005
[title] => 3M™ Half Face Reusable Respirator HF-52 with Holder 1700 And Filter 1744
[total] => 2
[head_quarter] => RAJAHMUNDRY
[0] => 2
)
[HYDERABAD] => Array
(
[unspcp_code] => 46182005
[title] => 3M™ 6200 HALF FACE MASK WITH 7093 FILTER
[total] => 2
[head_quarter] => HYDERABAD
[0] => 2
)
)
I want output like this :
output:
array
(
[RAJAHMUNDRY] => Array
(
[unspcp_code] => 46182005
[title] => 3M™ Half Face Reusable Respirator HF-52 with Holder 1700 And Filter 1744
[total] => 2
[head_quarter] => RAJAHMUNDRY
[0] => 2
)
)
)
array(
[HYDERABAD] => Array
(
[unspcp_code] => 46182005
[title] => 3M™ 6200 HALF FACE MASK WITH 7093 FILTER
[total] => 2
[head_quarter] => HYDERABAD
[0] => 2
)
)
I am not sure how you want to store those arrays, but let me help you.
I assume you have a datastructure like this, so one array with multiple values.
array (
key1 => ...values...,
key2 => ...values...,
...
key_n => ...values...
)
And you want something like this, si multiple arrays with single keys well you need to store that array somehow.
array (
key1 => ...values...
)
array (
key2 => ...values...
)
...
array (
key_n => ...values...
)
If you do not know the exact number of arrays, you can't $array1, $array2, ... $array_n and it also not efficent, so you shoudl have an array of arrays. So something like this:
array(
array (
key1 => ...values...
)
array (
key2 => ...values...
)
...
array (
key_n => ...values...
)
)
So you should iterate trough the keys of the input array and then
So the code
<?php
//example input array
$arr = array (
"key1" => "val1",
"key2" => "val2"
);
$keys = array_keys($arr); //get the keys of the input array, see phpdoc
$output = [];
foreach($keys as $key) {
$output[] = array ($arr[$key]);
}
?>
This will output an array of arrays, with single key of the inner array.
If this is not you answer, reply.
Research:
https://www.php.net/manual/en/function.array-keys.php
https://www.php.net/manual/en/control-structures.foreach.php
php.net - arrays manual Example #6 Accessing array elements
Maybe this document will help you
This may also help you
<?php
$stdArray = array(
"foo" => "bar",
42 => 24,
"dimensional" => array(
"fname" => "jon",
"lname" => "doe",
),
"multi" => array(
"RAJAHMUNDRY" => array(
"unspcp_code" => 46182005,
"head_quarter" => "RAJAHMUNDRY",
0 => 2
),
"HYDERABAD" => array(
"unspcp_code" => 46182005,
"head_quarter" => "HYDERABAD",
0 => 2
),
)
);
print_r($stdArray);
print_r($stdArray["multi"]);
print_r($stdArray["multi"]["RAJAHMUNDRY"]);

array push in multidimensional array when both object key same in PHP

I have twomultidimensional array in below format
$skillInfo = Array
(
"Mechanical" => Array
(
"0" => 100
"1" => 400
)
"Understanding" => Array
(
"0" => 200
)
"Application/Appreciation" => Array
(
"0" => 300
)
);
$skillMaster = [
["skillID" => 1, "skillName" => "Mechanical"],
["skillID" => 2, "skillName" => "Understanding"],
["skillID" => 3, "skillName" => "Application/Appreciation"]
];
$skillMaster skillName is the same as $skillInfo keys, so we can replace $skillInfo keys from $skillMaster skillID.
Expected output
Array
(
[1] => Array
(
[0] => 100
[1] => 400
)
[2] => Array
(
[0] => 200
)
[3] => Array
(
[0] => 300
)
)
my code
foreach($skillInfo as $key1 => $val1){
$skillInfo[$key1][$skillMaster[$val1["skillID"]]] = $skillMaster[$val1["skillID"]] ?? [];
}
above code is not working as expected output, kindly anyone help me out.
foreach($skillMaster as $item){
if(array_key_exists($item['skillName'], $skillInfo)){
$skillInfo[$item['skillID']] = $skillInfo[$item['skillName']];
unset($skillInfo[$item['skillName']]);
}
}

Change order of multidimensional array columns

I have a multidimensional array in php and i need to change the column order with the order of a second simple array.
EDIT:
Although both arrays are the same in regard of values and keys, im using this for export with phpexcel, and it generates the xls file with the order of the given array. I need to change that order so the xls file looks that way.
The array looks like this:
Array
(
[0] => Array
(
[name] => Name1
[sn] => Sn1
[somenumber] => 43234234
)
[1] => Array
(
[name] => Name2
[sn] => Sn2
[somenumber] => 4564564
)
[2] => Array
(
[name] => Name3
[sn] => Sn3
[somenumber] => 6575647456745
)
)
And the second array is this:
Array
(
[0] => sn
[1] => name
[2] => somenumber
)
What i need is the first array to be ordered based on the second so it looks like this:
Array
(
[0] => Array
(
[sn] => Name1
[name] => Sn1
[somenumber] => 43234234
)
[1] => Array
(
[sn] => Name2
[name] => Sn2
[somenumber] => 4564564
)
[2] => Array
(
[sn] => Name3
[name] => Sn3
[somenumber] => 6575647456745
)
)
this is how you can sort your array:
$template array:
//template array
$reference = array('sn', 'name', 'somenumber');
$array_to_sort = Array
(
"0" => Array
(
"somenumber" => "Name1",
"sn" => "Sn1",
"name" => "43234234"
),
"1" => Array
(
"sn" => "Name2",
"somenumber" => "4564564",
"name" => "Sn2"
),
"2" => Array
(
"sn" => "Name3",
"name" => "Sn3",
"somenumber" => "6575647456745"
)
);
$ordered_array = [];
foreach ($array_to_sort as $key => $value) {
$ordered_array[] = array_replace(array_flip($reference), $value);
}
print_r($ordered_array);
If all keys always present
// Make template array with correct order of keys
$template = array_flip($second);
foreach($array as &$x) {
// replace values in template
$x = array_replace($template, $x);
}
demo

Change index of multidimensional array PHP

I've tried some solutions from:
In PHP, how do you change the key of an array element?
php array from multidimensional array keys values
But its is not exacly what i need, i tried to mix some solutions but notting helped.
I have an array from my database:
Array (
[0] => Array (
[ID] => 1
[USER_ID] => 1
[DATA] => UNIQUE
[VALUE] => buuu )
[1] => Array (
[ID] => 2
[USER_ID] => 1
[DATA] => NICKNAME
[VALUE] => NoAd ) )
And i want to transform that database to:
Array (
[UNIQUE] => buuu
[NICKNAME] => NoAd
[any new [2]...[3]... from previous array
after that code:
foreach($playerdata as $segment){
foreach($segment as $key => $value ){
$newArray[$value] = $value;
}
}
my array looks like:
Array ( [UNIQUE] => UNIQUE
[buuu] => buuu
[NICKNAME] => NICKNAME
[NoAd] => NoAd )
i tried use 3x foreach but it ends in error all time i think i need to change some variables in my foreach but no idea how.
Now that I see the other answers it seems it's array_column you are looking for.
It returns an array column and the third parameter is what the key name should be.
$player_data = array(array(
"ID" => 1,
"USER_ID" => 1,
"DATA" => "UNIQUE",
"VALUE" => "buuu"
),
array(
"ID" => 1,
"USER_ID" => 1,
"DATA" => "NICKNAME",
"VALUE" => "NoAd"
));
$new = array_column($player_data, "VALUE", "DATA");
var_dump($new);
Output:
array(2) {
["UNIQUE"]=>
string(4) "buuu"
["NICKNAME"]=>
string(4) "NoAd"
}
https://3v4l.org/ZAkgZ
There is no need for loops to solve this.
lets assume $playerdata has the below values
Array (
[0] => Array (
[ID] => 1
[USER_ID] => 1
[DATA] => UNIQUE
[VALUE] => buuu )
[1] => Array (
[ID] => 2
[USER_ID] => 1
[DATA] => NICKNAME
[VALUE] => NoAd ) )
[2]----
[3]----
$newArray = [];
foreach($playerdata as $record) {
$newArray[$record['DATA']] = $record['DATA'];
$newArray[$record['VALUE']] = $record['VALUE'];
}
print_r($newArray);
You could try something like the following:
$newArray = array();
foreach($playerdata as $segment){
$newArray[$segment['DATA']] = $segment['VALUE'];
}
This code gets the DATA as key and VALUE as value from each part of the array and stores it in $newArray.

How to get index number of associative array that has a value in php

I have this associative array in php,
Array ( [items] => Array ( [0] => Array ( [Desc] => Tech Pen [Qty] => 15 ) [1] => Array ( [Desc] => Ballpen [Qty] => 4 ) ) )
How to get the index number that has a Description value of Tech Pen?
The answer should be, [0].
A possible solution to get your index number is to loop the $array using a foreach:
$array = array(
"items" => array(
array(
"Desc" => "Tech Pen",
"Qty" => 15
),
array(
"Desc" => "Ballpen",
"Qty" => 4
),
)
);
foreach ($array['items'] as $key => $item) {
if($item['Desc'] === 'Tech Pen') {
echo "The value 'Tech Pen' has index number: $key";
}
}
array_search() — Searches the array for a given value and returns the corresponding key if successful. Read More
Try:
$key=array_search('Tech Pen', $array);

Categories