How to print json data within array element as string - php

I have an Array Array
( [0] => ["1","2","7","8","9"] )
and i want to like that
Array ( [0] => 1 [1] => 2 [2] => 7 [3] => 8 [4] => 9 )
i try foreach loop
foreach($no_a as $key=>$val){
foreach($val as $k=>$v){
$newp_arr[] $v ;
}
}

$result = $no_a[0];
$new_result = array();
for ($i=0; $i < 21 ; $i++) {
# code...
$num = $result[$i];
if(is_numeric($num))
{
$new_result[] = $result[$i];
}
}
print_r($new_result);

Related

PHP array multidimensional cross all elements

I need to create a sql sentence from a dinamic structure. The structure come from a multidimensional array. It is more difficult explain that show it, so I show 3 examples:
Example1: If I have this array:
myarry Array
(
[5] => Array
(
[0] => 2
[1] => 5
)
[6] => Array
(
[0] => 11
)
)
I need to create a string like:
(2 AND 11) OR (5 AND 11)
Example2: If I have this array:
myarry Array
(
[5] => Array
(
[0] => 2
[1] => 5
)
[6] => Array
(
[0] => 11
[1] => 8
)
)
I need to create a string like:
(2 AND 11) OR (5 AND 11) OR (2 AND 8) OR (5 AND 8)
Example3: If I have this array:
myarry Array
(
[5] => Array
(
[0] => 2
[1] => 5
)
[6] => Array
(
[0] => 11
)
[7] => Array
(
[0] => 70
[1] => 71
[2] => 72
)
)
I need to create a string like:
(2 AND 11 AND 70) OR (2 AND 11 AND 71) OR (2 AND 11 AND 72) OR (5 AND 11 AND 70) OR (5 AND 11 AND 71) OR (5 AND 11 AND 72)
And so on...
The index on the array are not important.
I have tried already:
foreach ($myarry as $clave => $feature){
${"feat_$n"} = $feature;
$n++;
}
$quan= count($myarry);
foreach ($feat_0 as $feature1) {
for ($m = 1; $m < $quan; $m++){
$name = "feat_{$m}";
foreach ($$name as $feature2) {
echo "OR feature1: ".$feature1." AND feature2: ".$feature2."<br>";
}
}
}
And also:
foreach ($myarry as $clave => $feature){
${"feat_$n"} = $feature;
$n++;
}
$i = 0;
foreach ($feat_0 as $clave0 => $feature0) {
for ($m = 1; $m < $cantidad; $m++){
$name = "feat_{$m}";
foreach ($$name as $clave1 => $feature1) {
echo "-feature0: ".$feature0." - feature1: ".$feature1." - i: ".$i." - m: ".$m."<br>";
$i++;
if($m == 1)
$indice = $feature1;
else
$pena[$feature0][$indice][$i] = $feature1;
}
$i=0;
}
}
But I'm not even close to the solution :(
I hope the question is clear.
Any help will be welcome!
Here is the custom function from source with some modification,
First I created unique combinations of all the arrays elements like a set and then I mapped them to create the required string.
function custom_function($myarry)
{
if (count($myarry) == 0) {
return array();
}
$a = array_shift($myarry);
if (count($myarry) == 0) {
$c = array(array());
} else {
$c = custom_function($myarry); // recursive call
}
$r = array();
foreach ($a as $v) {
foreach ($c as $p) {
$r[] = array_merge(array($v), $p);
}
}
return $r;
}
$temp = custom_function($myarry);
$andArr = [];
array_walk($temp, function ($item, $key) use (&$andArr) {
$andArr[] = '(' . implode(" AND ", $item) . ') ';
});
$str = implode(' OR ', $andArr);
array_walk — Apply a user supplied function to every member of an array
array_shift — Shift an element off the beginning of an array
Demo.

How can I loop associative array respectively

I have following associative array:
Array
(
[0] => Array
(
[0] => 268
)
[1] => Array
(
[0] => 125
[1] => 258
[2] => 658
[3] => 128
[4] => 987
)
[2] => Array
(
[0] => 123
[1] => 258
)
[3] => Array
(
[0] => 168
)
)
I need the following result as string.
268
125258658128987
123258
168
What I have tried so far;
<?php
//consider my array is in $array variable
for ($i = 0; $i < count($array); $i++)
{
foreach ($array[$i] as $res)
{
echo $res . '<br/>';
}
}
?>
But unfortunately I am getting each numbers in a new line.
Any suggestion will be appreciated.
You have to echo the <br /> outside the foreach loop:
for ($i = 0; $i < count($array); $i++)
{
foreach ($array[$i] as $res) {
echo $res;
}
echo '<br />'; //<-- Put this outside the foreach loop
}
Or another option, you can use foreach and implode for a simpler approach
foreach ($array as $value)
{
echo implode('',$value);
echo '<br />';
}
Doc: implode()

Create an array from two array PHP

i have two arrays
$value_array = array('50','40','30','20','10');
$customer = array('300','200','100');
i want to distribute the value array to customers based on the value of customers that is taken as limit.adding values by checking it wont cross the limit that is 300 , 200 and 100.
but customer array not working one direction it should work first forward and then backward like that
i want to produce an array in form of
Array
(
[0] => Array
(
[0] => 50
)
[1] => Array
(
[0] => 40
[1] => 10
)
[2] => Array
(
[0] => 30
[1] => 20
)
)
After completing customer loop first time it should start from last to first. both array count will change , i mean count.
value array should check 50 -> 300 , 40->200, 30->100 then from last ie, 20 ->100, 10->200 etc.
I tried like
$i = 0;
while($i < count($customer)){
foreach($value_array as $k=>$value){
$v = 0;
if($value <= $customer[$i]){
$customer2[$i][] = $value;
unset($value_array[$k]);
$v = 1;
}
if($v ==1){
break;
}
}
//echo $i."<br/>";
if($i == (count($customer)-1) && (!empty($value_array))){
$i = 0;
$customer = array_reverse($customer, true);
}
$i++;
}
echo "<pre>";
print_r($customer2);
$valueArray = array('50','40','30','20','10','0','-11');
$customer = array('300','200','100');
function parse(array $valueArr, array $customerArr)
{
$customerCount = count($customerArr);
$chunkedValueArr = array_chunk($valueArr, $customerCount);
$temp = array_fill(0, $customerCount, array());
$i = 0;
foreach ($chunkedValueArr as $item) {
foreach ($item as $key => $value) {
$temp[$key][] = $value;
}
$temp = rotateArray($temp);
$i++;
}
// if $i is odd
if ($i & 1) {
$temp = rotateArray($temp);
}
return $temp;
}
function rotateArray(array $arr)
{
$rotatedArr = array();
//set the pointer to the last element and add it to the second array
array_push($rotatedArr, end($arr));
//while we have items, get the previous item and add it to the second array
for($i=0; $i<sizeof($arr)-1; $i++){
array_push($rotatedArr, prev($arr));
}
return $rotatedArr;
}
print_r(parse($valueArray, $customer));
returns:
Array
(
[0] => Array
(
[0] => 50
[1] => 0
[2] => -11
)
[1] => Array
(
[0] => 40
[1] => 10
)
[2] => Array
(
[0] => 30
[1] => 20
)
)

convert string to multidimensional array

I have this array
dev3->content->->mktg->->->pls1->->->pls2->->->config->->splash
I want to convert this string to multidimensional array. like this
Array
(
[0] => dev3
Array (
[0] => ->content
Array (
[0] => ->->mktg
Array(
[0] => ->->->pls1
[1] => ->->->pls2
[2] => ->->->config
)
[1] => ->->splash
)
)
)
Can anyone do this
it does not work if level will be increaed more then +1 on any step
$str = 'dev3->content->->mktg->->->pls1->->->pls2->->->config->->splash';
$in = preg_split('/(?<!>)(?=->)/', $str);
Above we make such array from the input string
Array
(
[0] => dev3
[1] => ->content
[2] => ->->mktg
[3] => ->->->pls1
[4] => ->->->pls2
[5] => ->->->config
[6] => ->->splash
)
continue working
$result = [];
$p = &$result;
$level = 0;
foreach($in as $i) {
// Count next level
$c = substr_count($i, '->');
// if level is not changed
if($c == $level) { $p[] = $i; continue; }
// level increased
if ($c == $level + 1) {
$level++;
$p[] = [$i];
$p = &$p[count($p)-1];
continue;
}
// any level less then achived before
if ($c < $level) {
$p = &$result;
$level = $c;
while($c--)
$p = &$p[count($p)-1];
$p[] = $i;
continue;
}
die("I can't process this input string");
}
print_r($result);
working demo

Associative index array to associative associative array

Problem
I have an array which is returned from PHPExcel via the following
<?php
require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
$excelFile = "excel/1240.xlsx";
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($excelFile);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$arrayData[$worksheet->getTitle()] = $worksheet->toArray();
}
print_r($arrayData);
?>
This returns:
Array
(
[Films] => Array
(
[0] => Array
(
[0] => Name
[1] => Rating
)
[1] => Array
(
[0] => Shawshank Redemption
[1] => 39
)
[2] => Array
(
[0] => A Clockwork Orange
[1] => 39
)
)
[Games] => Array
(
[0] => Array
(
[0] => Name
[1] => Rating
)
[1] => Array
(
[0] => F.E.A.R
[1] => 4
)
[2] => Array
(
[0] => World of Warcraft
[1] => 6
)
)
)
What I would like to have is
Array
(
[Films] => Array
(
[0] => Array
(
[Name] => Shawshank Redemption
[Rating] => 39
)
[1] => Array
(
[Name] => A Clockwork Orange
[Rating] => 39
)
)
[Games] => Array
(
[0] => Array
(
[Name] => F.E.A.R
[Rating] => 4
)
[1] => Array
(
[Name] => World of Warcraft
[Rating] => 6
)
)
)
The arrays names (Films, Games) are taken from the sheet name so the amount can be variable. The first sub-array will always contain the key names e.g. Films[0] and Games[0] and the amount of these can be varible. I (think I) know I will need to do something like below but I'm at a loss.
foreach ($arrayData as $value) {
foreach ($value as $rowKey => $rowValue) {
for ($i=0; $i <count($value) ; $i++) {
# code to add NAME[n] as keys
}
}
}
I have searched extensively here and else where if it is a duplicate I will remove it.
Thanks for any input
Try
$result= array();
foreach($arr as $key=>$value){
$keys = array_slice($value,0,1);
$values = array_slice($value,1);
foreach($values as $val){
$result[$key][] = array_combine($keys[0],$val);
}
}
See demo here
You may use nested array_map calls. Somehow like this:
$result = array_map(
function ($subarr) {
$names = array_shift($subarr);
return array_map(
function ($el) use ($names) {
return array_combine($names, $el);
},
$subarr
);
},
$array
);
Demo
Something like this should work:
$newArray = array();
foreach ($arrayData as $section => $list) {
$newArray[$section] = array();
$count = count($list);
for ($x = 1; $x < $count; $x++) {
$newArray[$section][] = array_combine($list[0], $list[$x]);
}
}
unset($arrayData, $section, $x);
Demo: http://ideone.com/ZmnFMM
Probably a little late answer, but it looks more like your tried solution
//Films,Games // Row Data
foreach ($arrayData as $type => $value)
{
$key1 = $value[0][0]; // Get the Name Key
$key2 = $value[0][1]; // Get the Rating Key
$count = count($value) - 1;
for ($i = 0; $i < $count; $i++)
{
/* Get the values from the i+1 row and put it in the ith row, with a set key */
$arrayData[$type][$i] = array(
$key1 => $value[$i + 1][0],
$key2 => $value[$i + 1][1],
);
}
unset($arrayData[$type][$count]); // Unset the last row since this will be repeated data
}
I think this will do:
foreach($arrayData as $key => $array){
for($i=0; $i<count($array[0]); $i++){
$indexes[$i]=$array[0][$i];
}
for($i=1; $i<count($array); $i++){
for($j=0; $j<count($array[$i]); $j++){
$temp_array[$indexes[$j]]=$array[$i][$j];
}
$new_array[$key][]=$temp_array;
}
}
print_r($new_array);
EDIT: tested and updated the code, works...

Categories