2

All values of array $A are string the same length.

$A  = Array
(
    [0] => 03
    [1] => 04
    [2] => 05
    [3] => 06
  //  [4] => 07 // "07" before "04" position
    [4] => 04
    [5] => 05
    [6] => 06
  //   [8] => 07 // "07" before "08" position
    [7] => 08
    [8] => 03
    [9] => 04
    [10] =>  05
    [11] => 06
    [12] => 07 // it is existing
    [13] => 08
) ; 

I want to Insert the "07" element if it is not existing before "04" or "08" position.start from position 1

So It will be after changed

$A  = Array
(
    [0] => 03
    [1] => 04
    [2] => 05
    [3] => 06
    [4] => 07 // just appended
    [5] => 04
    [6] => 05
    [7] => 06
    [8] => 07 // just append
    [9] => 08
    [10] => 03
    [11] => 04
    [12] =>  05
    [13] => 06
    [14] => 07
    [15] => 08
) ; 

Anybody know how to do this ,help me please?

3
  • 1
    "append" means to insert at the end. You are talking about insertion more generally. Commented Jun 9, 2011 at 15:36
  • @Tomalak I do believe he is talking about homework more generally... Commented Jun 9, 2011 at 15:38
  • 2
    @newbie can you explain why (in our expected result) 07 is only sometimes injected before 04, and not allways? Commented Jun 9, 2011 at 15:44

5 Answers 5

4

There would be "prettier" ways to do this but, as intended...

  1. iterate the array
  2. if the current value is equal to 7 minus 1 you will insert a new value there
  3. create a function "insert_into_array" that:
    a) Splits your array in two (look at array_chunk)
    b) POPs your element to the end of the first array (array_pop)
    c) merges your two arrays back (array_merge)

I've abstained from writing any code as this is probably homework and, writing code, even if you're not really deep thinking the problem will push you a long way to passing the exam...

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

Comments

2

not the most beautiful solution, but should do the job:

$b = array();
for($i=0;$i<count($A);$i++){
    $b[] = $A[$i];
    if(($i<count($A) - 1) && ($A[$i+1]<$A[$i] || ($A[$i+1] == '08')) && $A[$i] < '07')
        $b[] = '07';
}
var_dump($b);

1 Comment

THANS ,YOUR CONDITION if(($i<count($A) - 1) && ($A[$i+1]<$A[$i] || ($A[$i+1] == '08') ||$A[$i+1]==$A[$i] ) && $A[$i] < '07') IT'S OKAY.
0

First, find the gaps in your array, that is the positions where there's 06 but not a following 07:

$positions = array();
foreach ($A as $k => $v) {
    if (isset($last) && $last != $v - 1 && $last == '06') {
        $positions[] = $k;
    }
    $last = $v;
}

Then, insert them:

$count = 0;
foreach ($positions as $pos) {
    array_splice($A, $pos + ($count++), 0, '07');
}

That's it.

Comments

0
//make sure the array is numeric:
$A = array_values($A);
foreach(array('04','08') as $search){
    $positions = array_keys($A,$search);
    rsort($positions);
    foreach($positions as $key){
        if($key==0 || $A[$key-1] != '07'){
             array_splice($A,$key,0,'O7');
        }
    }
}

Comments

0

In 2017, I've found 2 beautiful methods that is part of nette\utils package.

They do job perfectly!

Just run:

composer require nette/utils

and use Arrays class or inspire in their code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.