there is my code, and after foreach loop is finished it doensn't look next code.
There is my code
ini_set('memory_limit', '2048M');
dump('in');
$imagesId = [];
$chunks = array_chunk($images, 10);
$j = 0;
$flag = false;
foreach($chunks as $img){
dump('-------------------');
$response = Http::withHeaders([
'Content-Type' => 'application/json',
])->withBasicAuth($ecart->client_key, $ecart->client_secret)->timeout(60)->put(
$ecart->url.'/wp-json/wc/v3/products/'.$wooId,
['images'=>$img]
);
$body = json_decode($response->getBody(), true);
for ($i = 0; $i < 10; $i++){
dump($j);
$imagesId[$j]['id']=$body['images'][$i]['id'];
$j++;
}
}
dump('out');
It doesn't return 'out', like it doesn't see this code.
Related
I have an image gallery and each is saved by index.. gallery1: /image here.. gallery2: / image here.. etc..
I'm using an index with multiple for loops to return the images and by column because it's either Masonry or rectangular. I am getting the return fine except for the very last index.
private function rectangle($items, $columns, $contents = array()) {
$thumbs = array('talent_thumbnail','360x207');
$arr = array();
$col = round(count($items) / $columns);
$perCol = floor(count($items) / $columns);
$extra = count($items) % $columns;
$ind = 1;
$length = count($items);
$arr = array();
for($i = 0; $i < $columns && $ind < $length; $i++) {
$temp = array();
for($j = 0; $j < $perCol; $j++) {
$obj = new JObject();
$obj->image = $items['gallery' . $ind]['photo'];
$obj->alt_text = $items['gallery'. $ind]['alt_text'];
$temp[] = $obj;
$ind++;
}
if ($extra > 0) {
$obj = new JObject();
$obj->image = $items['gallery'. $ind]['photo'];
$obj->alt_text = $items['gallery'. $ind]['alt_text'];
$temp[] = $obj;
$ind++;
$extra--;
}
$arr[] = $temp;
}
}
I know it can't be that hard but I'm not that good at it right yet.
Any help is so much welcome.
Thank You.
You set the variable $ind to 1, count the length of the array, and then initialize the for loop with the condition $ind < $length.
When $ind reaches the index needed to access the last item, the loop is not run again, since $ind is now equal to $length, not smaller.
You can fix this by changing the condition in your for-loop to "less or equal":
$i < $columns && $ind <= $length
This runs the loop once more when $ind has reached the last index.
Probably the problem is at this line:
$ind = 1;
In PHP non-associative arrays have the indexes starting to 0 instead of 1:
$arr = array('a', 'b', 'c');
print_r($arr);
// output:
Array ( [0] => a [1] => b [2] => c )
So, try to change that line in your code to:
$ind = 0;
Complete code:
private function rectangle($items, $columns, $contents = array()) {
$thumbs = array('talent_thumbnail','360x207');
$arr = array();
$col = round(count($items) / $columns);
$perCol = floor(count($items) / $columns);
$extra = count($items) % $columns;
$ind = 0;
$length = count($items);
$arr = array();
for($i = 0; $i < $columns && $ind < $length; $i++) {
$temp = array();
for($j = 0; $j < $perCol; $j++) {
$obj = new JObject();
$obj->image = $items['gallery' . $ind]['photo'];
$obj->alt_text = $items['gallery'. $ind]['alt_text'];
$temp[] = $obj;
$ind++;
}
if ($extra > 0) {
$obj = new JObject();
$obj->image = $items['gallery'. $ind]['photo'];
$obj->alt_text = $items['gallery'. $ind]['alt_text'];
$temp[] = $obj;
$ind++;
$extra--;
}
$arr[] = $temp;
}
}
I am trying to insert batch query in code igniter, I am not able to make array_merge work. Don't know whats the problem. M getting blank array.
$epin_amt = $this->input->post('amount');
$qty = $this->input->post('qty');
$data = array();
for ($i = 0; $i <= $qty; $i++) {
$array = array(
'epin' => mt_rand(100000, 999999),
'amount' => $epin_amt,
);
array_merge($data, $array);
}
print_r($data) ; // Produce : array( )
You have to assign the merged array back to your $data variable:
<?php
$epin_amt = /*$this->input->post('amount')*/ 5;
$qty = /*$this->input->post('qty')*/6;
$data = array();
for ($i = 0; $i <= $qty; $i++) {
$array = array(
'epin' => mt_rand(100000, 999999),
'amount' => $epin_amt,
);
$data = array_merge($data, $array);
}
print_r($data) ;
array_merge returns array. You need something like this:
$result = array_merge($data, $array);
You are merging the arrays but it's not done by reference so you're throwing the resulting array away. array_push() it instead, that will keep adding the arrays to your $data array:
<?php
$epin_amt = 10;
$qty = 20;
$data = array();
for ($i = 0; $i <= $qty; $i++) {
$array = array(
'epin' => mt_rand(100000, 999999),
'amount' => $epin_amt,
);
array_push($data, $array);
}
print_r($data) ;
This code in controller ISP should be once printed, but in here ISP follow script looping
$isp = $_POST['isp'];
if (isset($_POST['situs'])) {
$situs = $_POST['situs'];
for($i = 0; $i < count($situs); $i++)
{
$dataa['hasil'][] =(object) array('isp' => $isp,
'situs' => $situs[$i])
}
$sumber = $this->load->view('contoh', $dataa, TRUE);
This result :
I have problem with $res array.... i don't know with $res[0] is like a empty variable (array have a lot of dates but i don't know why and where is wrong... i tried with $res['ItemSlotX'] and now with $res[0]...
Edited: i have already changed my $res[0] and 1 but is same...
Thank you for help!
function smartsearch($whbin,$itemX,$itemY) {
if (substr($whbin,0,2)=='0x') $whbin=substr($whbin,2);
$items = str_repeat('0', 240);
$itemsm = str_repeat('1', 240);
$i = 0;
while ($i<240) {
$_item = substr($whbin,(64*$i), 64);
$type = (hexdec(substr($_item,18,2))/16);
$dbgetvalutslots = new DB_MSSQL;
$dbgetvalutslots->Database='DTRMUWAP';
$dbgetvalutslots->query("Select [ItemSlotX],[ItemSlotY] from ItemDetails where ItemIndex = '".hexdec(substr($_item,0,2))."' and ItemGroup = '".$type."'");
$ijj=0;
$res=array();
while ($dbgetvalutslots->next_record()) {
$temp = array(
'ItemSlotX' => $dbgetvalutslots->f('ItemSlotX'),
'ItemSlotY' => $dbgetvalutslots->f('ItemSlotY')
);
$res[$ijj]=$temp;
$ijj++;}
$y = 0;
while($y<$res[1]) {
$y++;
$x=0;
while($x<$res[0]) {
$items = substr_replace($items, '1', ($i+$x)+(($y-1)*8), 1);
$x++;
}
}
$i++;
}$y = 0;
while($y<$itemY) {
$y++;$x=0;
while($x<$itemX) {
$x++;
$spacerq[$x+(8*($y-1))] = true;
}
}
$walked = 0;
$i = 0;
while($i<120) {
if (isset($spacerq[$i])) {
$itemsm = substr_replace($itemsm, '0', $i-1, 1);
$last = $i;
$walked++;
}
if ($walked==count($spacerq)) $i=119;
$i++;}$useforlength = substr($itemsm,0,$last);
$findslotlikethis='^'.str_replace('++','+',str_replace('1','+[0-1]+', $useforlength));
$i=0;$nx=0;$ny=0;
while ($i<120) {
if ($nx==8) { $ny++; $nx=0; }
if ((eregi($findslotlikethis,substr($items, $i, strlen($useforlength)))) && ($itemX+$nx<9) && ($itemY+$ny<16))
return $i;
$i++;
$nx++;
}
return 1337;}
There seems to be a lack of context for the code in order to really understand it, but for what I can see here already, it should be:
Edit: some extra code to be more explicit
$y = 0;
$numberOfRecords = count($res);
while($recordN < $numberOfRecords)
while($y<$res[$recordN]["ItemSlotY"]) {
$y++;
$x=0;
while($x<$res[$recordN]["ItemSlotX"]) {
$items = substr_replace($items, '1', ($i+$x)+(($y-1)*8), 1);
$x++;
}
}
$i++;
$recordN++;
}
as $temp is an array and you're then doing $res[$ijj]=$temp;
I need to use Json array in the php code.
The problem is that I'm in a for loop and need to separate the array in 2 and then want to merge it. but so far it didn't work.
I use it to have a graph (jqxChart).
Here is my code
for($i = 0; $i < $nb; $i++){
if ($i%2 == 1){
$time[$i] = (hexdec($hour[$i]));
$orders1[] = array(
'OrderDate' => $time[$i],
);
}else{
$hour[$i] = $hour[$i] + 1;
$orders2[] = array(
'ProductName' => $hour[$i],
);
}
}
$orders[] = array_merge_recursive( $orders1[], $orders2[] );
}
echo json_encode($orders);
Thanks
try this code,
$orders1 = array();
$orders2 = array();
for($i = 0; $i < $nb; $i++){
if ($i%2 == 1){
....
$temp1 = array(
'OrderDate' => $time[$i],
);
array_push($orders1, $temp1);
}else{
....
$temp2 = array(
'ProductName' => $hour[$i],
);
array_push($orders2, $temp2);
}
}
}
$orders = array_merge( $orders1, $orders2 );
echo json_encode($orders);
Remove the square brackets. Instead of:
$orders[] = array_merge_recursive($orders1[], $orders2[]);
^^ ^^ ^^
Just put:
$orders = array_merge($orders1, $orders2);