-1

I am relatively new to php Sorry if I am not asking the question properly. here it goes:

My array:

$value = array(
        array('2500'),
        array('3000'),
        array('3500'),
        array('4000')
    );

From code:

$Array = array();
for ($i=0; $i<count($value);$i++) {
    $Array[]=$value;
} 
echo '<pre>';   
print_r($Array);
echo '</pre>';

How to merge it into one array like this:

Array
    [0] => 2500
    [1] => 3000
    [2] => 3500
    [3] => 4000
)

I've tried a lot of codes from array_merge to array_combine nothing seems to do the trick. Is their something I am missing in the code or is there a function or a filter that can accomplish this.

6
  • Instead of using sizeof which can be mistaken for something else make use of count. Commented Sep 4, 2017 at 10:42
  • 3v4l.org/lC1g8 Works for me. Though you seem to be inside another look at this for loop only looks around one value? Commented Sep 4, 2017 at 10:43
  • The variable $value is outputting like this : 2500300035004000 Commented Sep 4, 2017 at 10:47
  • What exactly is in $value? Your initial code block looks like four separate arrays, rather than one. Commented Sep 4, 2017 at 10:51
  • The $value is from advanced custom fields wordpress plugin. its actually a repeter get_sub_field(); Commented Sep 4, 2017 at 10:52

4 Answers 4

2

Try this:

<?php
$value = array(
        array('2500'),
        array('3000'),
        array('3500'),
        array('4000')
    );

echo '<pre>'; // for display purpose only
print_r($value); // for display purpose only
$array = array();
if (is_array($value)) {
    foreach ($value as $v) {
        $array = array_merge($array,$v);
    }
} else {
    $array = array($value);
}

print_r($array); // for display purpose only

EDITED based on OP's update

http://www.phpwin.org/s/ib6dOO

Sign up to request clarification or add additional context in comments.

6 Comments

Yes according to the updated question answer is correct but what if the $value is equal to this 2500300035004000
Strangely it still outputs this: Array ( [0] => 2500 ) Array ( [0] => 3000 ) Array ( [0] => 3500 ) Array ( [0] => 4000 ), Its like array is embedded into it.
This is the problem with your question without clear indication of $value. 1) set $array = array before your get_sub_field(); 2) to add the $value to the $array, then use $array = array_merge($array, $value);
Think I am doing it all wrong. Thanks for your help
Hi Michael Eugene Yuen, I got it working using this: $addmore_deadline_cat_fees = array(); if (have_rows('add_more_deadlines')): while (have_rows('add_more_deadlines')) : the_row(); $array = array(); if (have_rows('categories_and_fees')): while (have_rows('categories_and_fees')) : the_row(); $array_val = get_sub_field('fee'); array_push($array,$array_val); endwhile;endif; $addmore_deadline_cat_fees = array_merge($addmore_deadline_cat_fees, $array); endwhile;endif; print_r($addmore_deadline_cat_fees);
|
1

change

$Array[]=$value;

to

$Array[]=array_merge($value, $Array);

1 Comment

sorry didnt work output is like this: Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => )
1

You can use foreach to iterate through an array.

<?php

$data = array(
    array('2500'),
    array('3000'),
    array('3500'),
    array('4000')
);

$output = [];
foreach($data as $value) {
    $output[] = $value[0];
}

var_dump($output);

Output:

array (size=4)
  0 => string '2500' (length=4)
  1 => string '3000' (length=4)
  2 => string '3500' (length=4)
  3 => string '4000' (length=4)

You could alternatively run a function on each member of the array using array_map. array_pop returns the last element 'popped' off each sub-array.

$output = array_map('array_pop', $data);
var_dump($output);

Output:

array (size=4)
  0 => string '2500' (length=4)
  1 => string '3000' (length=4)
  2 => string '3500' (length=4)
  3 => string '4000' (length=4)

Comments

0

You can try this

$output = array_column($value, 0);
print_r($output);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.