Convert an array of array into one array - php

I have a problem with my array, So my array is :
Array
(
[0] => Array
(
[0] => Array
(
[sValue] => 1
)
[1] => Array
(
[sValue] => 2
)
)
)
I want to get this array :
Array
(
[0]=>1
[1]=>2
)
I tried like this, but not work, it's get only the sValue = 1:
for($i=0;$i<count($aExpectedAnswers);$i++){
foreach($aExpectedAnswers as $answer){
$aFormatedAnswers[] = '\''.$answer[$i]['sValue'].'\'';
}
}
Help me please, Thx in advance

$aFormatedAnswers = [];
foreach ($aExpectedAnswers as $answer) {
if (is_array($answer)) {
foreach ($answer as $item) {
$aFormatedAnswers[] = $item;
}
} else {
$aFormatedAnswers[] = $answer;
}

$result = array();
foreach($initial as $subArray){
foreach($subArrray as $value){
$result[] = $value;
}
}
print_r($result);

try this code:
$aExpectedAnswers = array(
array(
0 => array('sValue'=>1),
1 => array('sValue'=>2),
)
);
$result = array();
foreach($aExpectedAnswers as $aea){
foreach($aea as $ae){
$result[] = $ae['sValue'];
}
}
print_r($result);
hopefully helping.

Related

Remove items from multidimensional array in PHP

I need to remove empty items in a multidimensional array.
Is there a simple way I can remove the empty items easily?
I need to keep only 2010-06 and 2010-07.
Thank you very much!
Array
(
[2010-01] => Array
(
[2010-03] => Array
(
[0] =>
)
[2010-04] => Array
(
[0] =>
)
[2010-06] => Array
(
[0] => stdClass Object
(
[data_test] => value
[date] => 2010-05-01 12:00:00
)
)
[2010-07] => Array
(
[0] => stdClass Object
(
[data_test] => value
[date] => 2010-05-01 12:00:00
)
)
)
)
Try this Function. This will solve your issue.
function cleanArray($array)
{
if (is_array($array))
{
foreach ($array as $key => $sub_array)
{
$result = cleanArray($sub_array);
if ($result === false)
{
unset($array[$key]);
}
else
{
$array[$key] = $result;
}
}
}
if (empty($array))
{
return false;
}
return $array;
}
array_filter will not wrok with this array
so try this custom function
<?php
$array =array(
20 => array(
20 => array(
0=> ''
),
10 => array(
0=> 'hey'
)
)
);
function array_remove_empty($arr){
$narr = array();
while(list($key, $val) = each($arr)){
if (is_array($val)){
$val = array_remove_empty($val);
// does the result array contain anything?
if (count($val)!=0){
// yes :-)
$narr[$key] = $val;
}
}
else {
if (trim($val) != ""){
$narr[$key] = $val;
}
}
}
unset($arr);
return $narr;
}
print_r(array_remove_empty($array));
?>
found this answer here

How to remove duplicate values from a multidimentional array according to a key

Simple question have following code but won't work
//Enter your code here, enjoy!
$correo[] = array('valor'=>'vikram.123#gmil.com','registro'=>'23');
$correo[] = array('valor'=>'vikram.123#gmil.com','registro'=>'24');
$correo[] = array('valor'=>'vikram.123#gmil.com','registro'=>'26');
function arrayUnique($myArray){
if(!is_array($myArray))
return $myArray;
foreach ($myArray as &$myvalue){
$myvalue=serialize($myvalue);
}
$myArray=array_unique($myArray);
foreach ($myArray as &$myvalue){
$myvalue=unserialize($myvalue);
}
return $myArray;
}
$arr= arrayUnique($correo);
var_dump($arr);
What I am trying to do is remove those row which has repetitive valor key for example the result of above array should be
Array (
[0] => Array (
[valor] => 'vikram.123#gmail.com'
[registro] => 24
)
)
You can do like this...
$correo[] = array('valor'=>'vikram.123#gmil.com','registro'=>'23');
$correo[] = array('valor'=>'vikram.123#gmil.com','registro'=>'24');
$correo[] = array('valor'=>'vikram.123#gmil.com','registro'=>'26');
function arrayUnique($myArray, $uniqueKeyName)
{
// Unique Array for return
$myReturnArray = array();
// Array with the md5 hashes
$arrayHashes = array();
foreach($myArray as $key => $item) {
$hash = md5(serialize($item[$uniqueKeyName]));
if (!isset($arrayHashes[$hash])) {
$arrayHashes[$hash] = $hash;
$myReturnArray[] = $item;
}
}
return $myReturnArray;
}
$arr= arrayUnique($correo, 'valor');
echo "<pre>";
print_r($arr);
echo "</pre>";
You may try like this, since you said any one of registro should be considered.
[akshay#localhost tmp]$ cat test.php
<?php
$array = array(
array('valor'=>'vikram.123#gmil.com','registro'=>'23'),
array('valor'=>'vikram.123#gmil.com','registro'=>'24'),
array('valor'=>'vikram.123#gmil.com','registro'=>'26')
);
function remove_dup($array, $key)
{
foreach( $array as $v )
{
if(! isset($out[ $v[$key] ] ) )
$out[$v[$key]] = $v;
}
return array_values($out);
}
// Input
print_r( $array );
// Output
print_r( remove_dup($array,'valor') );
?>
Output
[akshay#localhost tmp]$ php test.php
Array
(
[0] => Array
(
[valor] => vikram.123#gmil.com
[registro] => 23
)
[1] => Array
(
[valor] => vikram.123#gmil.com
[registro] => 24
)
[2] => Array
(
[valor] => vikram.123#gmil.com
[registro] => 26
)
)
Array
(
[0] => Array
(
[valor] => vikram.123#gmil.com
[registro] => 23
)
)
You can define the function like -
function arrayUnique($myArray){
if(!is_array($myArray))
return $myArray;
$temp = array();
foreach ($myArray as $myvalue){
$temp[$myvalue['valor']] = $myvalue;
}
return $temp;
}
Store the array with valor as key. It will store only the last one.

Covert flat array to Nested Array in PHP

Given the following input:
array('one/two/3',
'one/four/0/5',
'one/four/1/6',
'one/four/2/7',
'eight/nine/ten/11')
How can I convert it into this:
array(
'one': array(
'two': 3,
'four': array(5,6,7)
)
'eight': array(
'nine': (
'ten':11
)
}
)
$input = array ('one/two/3',
'one/four/0/5',
'one/four/1/6',
'one/four/2/7',
'eight/nine/ten/11');
$result = array ();
foreach ($input as $string) {
$data = array_reverse(explode('/', $string));
$tmp_array = array ();
foreach ($data as $val) {
if (empty($tmp_array)) {
$tmp_array = $val;
} else {
$tmp = $tmp_array;
$tmp_array = array ();
$tmp_array[$val] = $tmp;
}
}
$result = array_merge_recursive($result, $tmp_array);
}
echo "<pre>";
print_r($result);
echo "</pre>";
Output:
Array
(
[one] => Array
(
[two] => 3
[four] => Array
(
[0] => 5
[1] => 6
[2] => 7
)
)
[eight] => Array
(
[nine] => Array
(
[ten] => 11
)
)
)
It would be nice if we saw what you have tried.
$my_array = array('one/two/3',
'one/four/0/5',
'one/four/1/6',
'one/four/2/7',
'eight/nine/ten/11');
$result= array();
foreach ($my_array as $val) {
$ref = & $result;
foreach (explode("/", $val) as $val) {
if (!isset($ref[$val])) {
$ref[$val] = array();
}
$ref = & $ref[$val];
}
$ref = $val;
}
var_dump($result);

count of duplicate elements in an array in php

Hi,
How can we find the count of duplicate elements in a multidimensional array ?
I have an array like this
Array
(
[0] => Array
(
[lid] => 192
[lname] => sdsss
)
[1] => Array
(
[lid] => 202
[lname] => testing
)
[2] => Array
(
[lid] => 192
[lname] => sdsss
)
[3] => Array
(
[lid] => 202
[lname] => testing
)
)
How to find the count of each elements ?
i.e, count of entries with id 192,202 etc
You can adopt this trick; map each item of the array (which is an array itself) to its respective ['lid'] member and then use array_count_value() to do the counting for you.
array_count_values(array_map(function($item) {
return $item['lid'];
}, $arr);
Plus, it's a one-liner, thus adding to elite hacker status.
Update
Since 5.5 you can shorten it to:
array_count_values(array_column($arr, 'lid'));
foreach ($array as $value)
{
$numbers[$value[lid]]++;
}
foreach ($numbers as $key => $value)
{
echo 'numbers of '.$key.' equal '.$value.'<br/>';
}
Following code will count duplicate element of an array.Please review it and try this code
$arrayChars=array("green","red","yellow","green","red","yellow","green");
$arrLength=count($arrayChars);
$elementCount=array();
for($i=0;$i<$arrLength-1;$i++)
{
$key=$arrayChars[$i];
if($elementCount[$key]>=1)
{
$elementCount[$key]++;
} else {
$elementCount[$key]=1;
}
}
echo "<pre>";
print_r($elementCount);
OUTPUT:
Array
(
[green] => 3
[red] => 2
[yellow] => 2
)
You can also view similar questions with array handling on following link
http://solvemyquest.com/count-duplicant-element-array-php-without-using-built-function/
The following code will get the counts for all of them - anything > 1 at the end will be repeated.
<?php
$lidCount = array();
$lnameCount = array();
foreach ($yourArray as $arr) {
if (isset($lidCount[$arr['lid']])) {
$lidCount[$arr['lid']]++;
} else {
$lidCount[$arr['lid']] = 1;
}
if (isset($lnameCount [$arr['lname']])) {
$lnameCount [$arr['lname']]++;
} else {
$lnameCount [$arr['lname']] = 1;
}
}
$array = array('192', '202', '192', '202');
print_r(array_count_values($array));
$orders = array(
array(
'lid' => '',
'lname' => '',
))....
$foundIds = array();
foreach ( $orders as $index => $order )
{
if ( isset( $foundIds[$order['lid']] ) )
{
$orders[$index]['is_dupe'] = true;
$orders[$foundIds[$order['lid']]]['is_dupe'] = true;
} else {
$orders[$index]['is_dupe'] = false;
}
$foundIds[$order['lid']] = $index;
}
Try this code :
$array_count = array();
foreach ($array as $arr) :
if (in_array($arr, $array_count)) {
foreach ($array_count as $key => $count) :
if ($key == $arr) {
$array_count[$key]++;
break;
}
endforeach;
} else {
$array_count[$arr] = 1;
}
endforeach;
Check with in_array() function.

help needed restructuring a php array

I was wondering if anyone could help me restructure a predefined php array. The output of my current array is:
Array
(
[71-ctns] => 1
[71-units] => 1
[308-units] => 1
[305-ctns] => 1
[306-units] => 2
)
And I would like it to look like:
Array
(
[71] => Array
(
[ctns] => 1
[units] => 1
)
[308] => Array
(
[units] => 1
)
[305] => Array
(
[ctns] => 1
)
[306] => Array
(
[units] => 2
)
)
Is this possible?
This should do it
$merged = array();
foreach($a as $k=>$v){
$t = explode('-',$k);
$id = intval($t[0]);
if(!array_key_exists($id, $merged))
$merged[$id] = array();
$merged[$id][$t[1]] = $v;
}
EDIT:
Sorry you should use explode instead of split.
Yes, but you need to loop (note: array_map can also work, but this example is more explicit):
$fin = array();
foreach( $complex as $item => $val )
{
$pieces = explode('-', $item);
$fin[$pieces[0]] = isset($fin[$pieces[0]])?:array();
$fin[$pieces[0]][$pieces[1]] = $val;
}
Find below code to restructure a predefined php array
<?php
$newArray=array();
$result = array("71-ctns"=>1,"71-units"=>1,"308-ctns"=>1,"308-units"=>1,"305-units"=>1,"306-units"=>2);
if(is_array($result) && count($result)>0) {
foreach($result as $key=>$val) {
$getKeyArray = explode("-",$key);
$newArray[$getKeyArray[0]][$getKeyArray[1]] =$val;
}
}
print"<pre>";
print_r($newArray);
?>

Categories